//
//  TPGetGoldView.m
//  TangPoetry
//
//  Created by 虎爷 on 2019/11/7.
//  Copyright © 2019 虎爷. All rights reserved.
//

#import "GPTipView.h"
#import "ZRBirthdayHeartView.h"
@import AudioToolbox;
@interface GPTipView ()
@property (weak, nonatomic) IBOutlet UILabel *amount_lab;
@property (weak, nonatomic) IBOutlet UIImageView *goldBG_img;
@property (weak, nonatomic) IBOutlet UILabel *tipNameLab;
@property (weak, nonatomic) IBOutlet UIImageView *tipImg;
@property (nonatomic, strong) IBOutlet ZRBirthdayHeartView *birthdayView;
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *birthdayViewHeightConstraint;
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *birthdayViewWidthConstraint;

@property (assign,nonatomic) NSInteger closeTimeNum;
@property (nonatomic, weak) NSTimer *closeTipTimer;

@property (assign, nonatomic) SystemSoundID foldSound;
@end
@implementation GPTipView


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.closeTimeNum = 3;
    self.closeTipTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(closeTipTimerHeadle) userInfo:nil repeats:YES];
    
    [self setGoldBGAnimation];
    
    [[JPAccount sharedJPAccount]LoadSwitchFromSandbox];
    NSNumber *wx_switch = [JPAccount sharedJPAccount].wx_switch;
    if ([wx_switch boolValue]) {
        [self.double_btn setHidden:NO];
    }else{
        [self.double_btn setHidden:YES];
    }
    [self configureSounds];
}



- (void)setAmount:(NSNumber *)amount{
    _amount = amount;
    self.amount_lab.text = [NSString stringWithFormat:@"%@",amount];
}
- (void)setName:(NSString *)name{
    _name = name;
    
    
    if ([name isEqualToString:@"升级"]) {
        self.tipNameLab.text = @"钻石";
        [self.birthdayView setLevel:[NSString stringWithFormat:@"%d",self.level.intValue + 1]];
        self.birthdayViewHeightConstraint.constant = self.birthdayView.height;
        self.birthdayViewWidthConstraint.constant = self.birthdayView.width;
        [self handleBirthdayViewEvent];
        [self.birthdayView setHidden:NO];
    }else{
        self.tipNameLab.text = name;
        [self.birthdayView setHidden:YES];
    }
    
    self.tipImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@背景",name]];
}
+ (instancetype)initGPTipView{
    return NSbunleloadNibName(@"GPTipView");
}

- (void)configureSounds
{
    // Configure bloom sound
    //
    NSString *foldSoundPath = [[NSBundle mainBundle]pathForResource:@"fold" ofType:@"caf"];
    NSURL *foldSoundURL = [NSURL fileURLWithPath:foldSoundPath];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)foldSoundURL, &_foldSound);
}
- (IBAction)closeClick:(id)sender {
    AudioServicesPlaySystemSound(self.foldSound);
    !self.closeCilckBlock ?: self.closeCilckBlock();
    [self removeFromSuperview];
}

- (IBAction)doubleClick:(id)sender {
    [self setHidden:YES];
    self.doubleClickBlock(self.bonusesTypeId);
}



- (void)handleBirthdayViewEvent {
    
    
    [self.birthdayView animationForBirthdayLid];
    WEAKSELF
    self.birthdayView.receiveActionBlock = ^{
//        typeof(self) strongSelf = weakSelf;
//        if (strongSelf && strongSelf.receiveActionBlock) {
//            strongSelf.receiveActionBlock();
//        }
        weakSelf.birthdayView.isCloseAnimation = YES;
        AudioServicesPlaySystemSound(weakSelf.foldSound);
        !weakSelf.closeCilckBlock ?: weakSelf.closeCilckBlock();
        [weakSelf removeFromSuperview];
    };
}


-(void)closeTipTimerHeadle{
    if (self.closeTimeNum <= 0) {
        [self.closeTipTimer invalidate];
        self.closeTipTimer = nil;
        
        [self.close_btn setTitle:@"点击关闭" forState:UIControlStateNormal];
        self.close_btn.userInteractionEnabled = YES;
    }else{
        [self.close_btn setTitle:[NSString stringWithFormat:@"%ld秒后关闭",(long)self.closeTimeNum] forState:UIControlStateNormal];
        self.closeTimeNum--;
        self.close_btn.userInteractionEnabled = NO;
    }
}



-(void)setGoldBGAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue = [NSNumber numberWithFloat: M_PI *2];
    animation.duration = 3;
    animation.autoreverses = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    [self.goldBG_img.layer addAnimation:animation forKey:nil];
}




@end
